package com.ruoyi.dida.db.classify.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.dida.db.classify.domain.CatalogueClassify;
import com.ruoyi.dida.db.classify.service.ICatalogueClassifyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 数据仓库目录Controller
 * 
 * @author ruoyi
 * @date 2020-08-12
 */
@Controller
@RequestMapping("/catalogue/classify")
public class CatalogueClassifyController extends BaseController {
    private String prefix = "catalogue/classify";

    @Autowired
    private ICatalogueClassifyService catalogueClassifyService;

    @RequiresPermissions("catalogue:classify:view")
    @GetMapping()
    public String classify()
    {
        return prefix + "/classify";
    }

    /**
     * 查询数据仓库目录树列表
     */
    @RequiresPermissions("catalogue:classify:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CatalogueClassify> list(CatalogueClassify catalogueClassify) {
        List<CatalogueClassify> list = catalogueClassifyService.selectCatalogueClassifyList(catalogueClassify);
        return list;
    }

    /**
     * 导出数据仓库目录列表
     */
    @RequiresPermissions("catalogue:classify:export")
    @Log(title = "数据仓库目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CatalogueClassify catalogueClassify)
    {
        List<CatalogueClassify> list = catalogueClassifyService.selectCatalogueClassifyList(catalogueClassify);
        ExcelUtil<CatalogueClassify> util = new ExcelUtil<CatalogueClassify>(CatalogueClassify.class);
        return util.exportExcel(list, "classify");
    }

    /**
     * 新增数据仓库目录
     */
    @GetMapping(value = { "/add/{catalogueId}", "/add/" })
    public String add(@PathVariable(value = "catalogueId", required = false) Long catalogueId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(catalogueId))
        {
            mmap.put("catalogueClassify", catalogueClassifyService.selectCatalogueClassifyById(catalogueId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存数据仓库目录
     */
    @RequiresPermissions("catalogue:classify:add")
    @Log(title = "数据仓库目录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CatalogueClassify catalogueClassify)
    {
        return toAjax(catalogueClassifyService.insertCatalogueClassify(catalogueClassify));
    }

    /**
     * 修改数据仓库目录
     */
    @GetMapping("/edit/{catalogueId}")
    public String edit(@PathVariable("catalogueId") Long catalogueId, ModelMap mmap)
    {
        CatalogueClassify catalogueClassify = catalogueClassifyService.selectCatalogueClassifyById(catalogueId);
        mmap.put("catalogueClassify", catalogueClassify);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据仓库目录
     */
    @RequiresPermissions("catalogue:classify:edit")
    @Log(title = "数据仓库目录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CatalogueClassify catalogueClassify)
    {
        return toAjax(catalogueClassifyService.updateCatalogueClassify(catalogueClassify));
    }

    /**
     * 删除
     */
    @RequiresPermissions("catalogue:classify:remove")
    @Log(title = "数据仓库目录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{catalogueId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("catalogueId") Long catalogueId)
    {
        return toAjax(catalogueClassifyService.deleteCatalogueClassifyById(catalogueId));
    }

    /**
     * 选择数据仓库目录树
     */
    @GetMapping(value = { "/selectClassifyTree/{catalogueId}", "/selectClassifyTree/" })
    public String selectClassifyTree(@PathVariable(value = "catalogueId", required = false) Long catalogueId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(catalogueId))
        {
            mmap.put("catalogueClassify", catalogueClassifyService.selectCatalogueClassifyById(catalogueId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载数据仓库目录树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = catalogueClassifyService.selectCatalogueClassifyTree();
        return ztrees;
    }
}
